package com.example.administrator.im.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.im.R;
import com.example.administrator.weather.gson.Forecast;
import com.example.administrator.weather.gson.Weather;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * create:2018/10/28 18:37
 * author: 姚鸿杰
 **/
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.Holder> {


    private Context mContext;
    private Weather weather;

    public WeatherAdapter(Context mContext, Weather mList) {
        this.mContext = mContext;
        this.weather = mList;
    }

    public void setmList(Weather weather) {
        this.weather = weather;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).
                inflate(R.layout.recycler_weather, viewGroup, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {

        Forecast forecast = weather.forecastList.get(i);
        String date = "";

        switch (i) {
            case 0:
                date = "今天";
                break;
            case 1:
                date = "明天";
                break;
            case 2:
                Date time = new Date();
                SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
                date = dateFm.format(time);
                String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
                String[] week_days = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
                for (int j = 0; j < weekDays.length; j++) {
                    if (date.equals(weekDays[j])) {
                        int k = j;
                        if (j == 5) k = 0;
                        else if (j == 6) k = 1;
                        else k = j + 2;
                        date = week_days[k];
                        break;
                    }
                }
                break;
        }
        holder.tv_date.setText(date + " • " + forecast.more.info);

        //根据天气修改
        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.sunny);

        // 这一步必须要做,否则不会显示.
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        holder.temperature.setCompoundDrawables(drawable, null, null, null);
        holder.temperature.setText(forecast.temperature.min + "-" + forecast.temperature.max);

    }

    @Override
    public int getItemCount() {
        if (weather == null)
            return 0;
        return weather.forecastList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private TextView tv_date;
        private TextView temperature;

        public Holder(@NonNull View itemView) {
            super(itemView);

            tv_date = (TextView) itemView.findViewById(R.id.tv_date);
            temperature = (TextView) itemView.findViewById(R.id.temperature);
        }
    }
}
